package utils

import (
	"encoding/binary"
	"math"
	"os"
	"os/exec"
	"path/filepath"
)

func StringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

func Float64FromBytes(bytes []byte) float64 {
	bits := binary.LittleEndian.Uint64(bytes)
	float := math.Float64frombits(bits)
	return float
}

func Float64ToBytes(float float64) []byte {
	bits := math.Float64bits(float)
	bytes := make([]byte, 8)
	binary.LittleEndian.PutUint64(bytes, bits)
	return bytes
}

//ResultOfCmd 执行简单的命令，并获取输出（标准输出与错误输出）
func ResultOfCmd(c string, params ...string) (string, int) {
	cmd := exec.Command(c, params...)
	cmdOutput, _ := cmd.CombinedOutput()
	return string(cmdOutput), cmd.ProcessState.ExitCode()
}

//CurrentExePath 返回当前进程所在的绝对目录，以路径分割符号结尾
func CurrentExePath() string {
	if ex, err := os.Executable(); err == nil {
		return filepath.Dir(ex) + "/"
	}
	return "./"
}
